﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.VungKhuvuc
{
    public partial class popThemkhuvuc : Form
    {
        public popThemkhuvuc()
        {
            InitializeComponent();
        }

        private void binCombo(ComboBox cb, DataTable dt, string name, string value, string option)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;
            ls = new trangthai();
            ls.name = option;
            ls.value = "0";
            list.Add(ls);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ls = new trangthai();
                    ls.name = dt.Rows[i][name].ToString();
                    ls.value = dt.Rows[i][value].ToString();
                    list.Add(ls);
                }
            }
            cb.DataSource = list;
            cb.DisplayMember = "name";
            cb.ValueMember = "value";
        }

        private void popThemkhuvuc_Load(object sender, EventArgs e)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;

            ls = new trangthai();
            ls.name = "Hoạt động";
            ls.value = "1";
            list.Add(ls);

            ls = new trangthai();
            ls.name = "Không hoạt động";
            ls.value = "0";
            list.Add(ls);

            cbtrangthai.DataSource = list;
            cbtrangthai.DisplayMember = "name";
            cbtrangthai.ValueMember = "value";
            //
            adminVung clv = new adminVung();
            DataTable dtv = clv.getdata(1);
            binCombo(cbvung, dtv, "tenVung", "id", "<Chọn vùng>");
            //binCombo(cbttp, dtttp, "ten", "id", "<Chọn tỉnh/thành phố>");
            //if (Int16.Parse(dtdetail.Rows[0]["idTP"].ToString()) > 0)
            //    cbttp.SelectedValue = dtdetail.Rows[0]["idTP"].ToString();
        }

        // khai báo 1 hàm delegate
        public delegate void ThemKV(int outid);
        // khai báo 1 kiểu hàm delegate
        public ThemKV MyFunction;

        private void btsave_Click(object sender, EventArgs e)
        {
            if (MyFunction != null)
            {// tại đây gọi nó
                Int16 idv = Int16.Parse((cbvung.SelectedValue == null) ? "0" : cbvung.SelectedValue.ToString());
                string tenkv = txttenkv.Text.Trim();
                string mota = txtmota.Text.Trim();
                Int16 trangthai = Int16.Parse((cbtrangthai.SelectedValue == null) ? "0" : cbtrangthai.SelectedValue.ToString());
                adminKhuvuc adkv = new adminKhuvuc();
                int outid = -1;
                if (idv > 0)
                    outid = adkv.them(idv, tenkv, mota, trangthai);
                MyFunction(outid);
            }
            this.Close();
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
